package com.zhdl.modules.prefiresys.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.IdUtil;
import com.project.network.entity.PortComm;
import com.project.network.service.PortCommService;
import com.zhdl.common.util.WebResult;
import com.zhdl.modules.prefiresys.service.dto.BaseQueryCriteria;
import com.zhdl.modules.prefiresys.web.request.ComSerialInsertRequest;
import com.zhdl.modules.prefiresys.web.request.ComSerialUpdateRequest;
import com.zhdl.modules.prefiresys.web.response.ComSerialResp;
import com.zhdl.modules.prefiresys.web.service.PorComServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 串口CRUD相关接口
 */
@RestController
@RequestMapping("/api/v1/sp")
public class ComSerialController {
    @Autowired
    PortCommService portCommService;

    @Autowired
    PorComServiceImpl porComService;

    /**
     * 查询列表
     *
     * @return
     */
    @PostMapping(path = "/lists")
    public WebResult<List<PortComm>> getLists() {
        BaseQueryCriteria bc = new BaseQueryCriteria();
        List<PortComm> list = portCommService.findList(bc);
        return WebResult.okResult(list);
    }
    /**
     * 查询列表
     *
     * @return
     */
    @PostMapping(path = "/list")
    public WebResult<List<ComSerialResp>> getList() {
        List<ComSerialResp> allComList = porComService.getAllComList();
        return WebResult.okResult(allComList);
    }

    /**
     * 添加
     *
     * @param request
     * @return
     */
    @PostMapping(path = "insert")
    public WebResult<PortComm> insert(@RequestBody @Validated ComSerialInsertRequest request) {
        PortComm entity = new PortComm();
        BeanUtil.copyProperties(request, entity, CopyOptions.create().setIgnoreNullValue(true));
        entity.setId(IdUtil.fastSimpleUUID());
        PortComm newDto = portCommService.insert(entity);

        return WebResult.okResult(newDto);
    }

    /**
     * 编辑
     *
     * @param request
     * @return
     */
    @PostMapping(path = "update")
    public WebResult<PortComm> update(@RequestBody @Validated ComSerialUpdateRequest request) {
        PortComm entity = new PortComm();
        BeanUtil.copyProperties(request, entity, CopyOptions.create().setIgnoreNullValue(true));
        PortComm newDto = portCommService.update(entity, request.getId());
        return WebResult.okResult(newDto);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "delete/{id}")
    public WebResult deleteById(@PathVariable("id") String id) {
        portCommService.deleteById(id);
        return WebResult.okResult();
    }

    /**
     * 根据ID查询某一个
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/getById/{id}")
    public WebResult<PortComm> selectById(@PathVariable String id) {
        PortComm portComm = portCommService.selectById(id);
        return WebResult.okResult(portComm);
    }
}
